using UnityEngine;
using QFramework;
using UnityEngine.Tilemaps;
using JetBrains.Annotations;

namespace ProjectSurviver
{
	public partial class RepeatTileController : ViewController
	{
		// #if UNITY_EDITOR
        // [UnityEditor.CustomEditor(typeof(RepeatTileController))]
        // public class RepeatTileControllerEditor:UnityEditor.Editor
        // {
            
        //     public override void OnInspectorGUI()
        //     {
        //         base.OnInspectorGUI();
        //         if (GUILayout.Button("重新计算 Bounds"))
        //         {
        //             var controller=target as RepeatTileController;
        //             controller.Tilemap.CompressBounds();
        //         }
        //     }
        // }
		// #endif

		private Tilemap mUp;
		private Tilemap mDown;
		private Tilemap mLeft;
		private Tilemap mRight;
		private Tilemap mUpLeft;
		private Tilemap mUpRight;
		private Tilemap mDownLeft;
		private Tilemap mDownRight;
		private Tilemap mCenter;

		private int AreaX = 0;
		private int AreaY = 0;

        void Start()
        {
            CreateTilemaps();
			UpDatePositions();
        }

        void Update()
        {
            if(Player.Default && Time.frameCount % 60 == 0)
			{
				var cellPos = Tilemap.layoutGrid.WorldToCell(Player.Default.transform.Position()); // 获取玩家所在的格子位置
				AreaX = cellPos.x / Tilemap.size.x; // 计算格子所在的区域
				AreaY = cellPos.y / Tilemap.size.y;
				UpDatePositions(); // 更新位置
			}
        }

        void CreateTilemaps()
        {
            mUp = Tilemap.InstantiateWithParent(transform); // 实例化一个Tilemap对象，并将其作为当前对象的子对象
            mDown = Tilemap.InstantiateWithParent(transform); 
            mLeft = Tilemap.InstantiateWithParent(transform);
            mRight = Tilemap.InstantiateWithParent(transform);
            mUpLeft = Tilemap.InstantiateWithParent(transform);
            mUpRight = Tilemap.InstantiateWithParent(transform);
            mDownLeft = Tilemap.InstantiateWithParent(transform);
            mDownRight = Tilemap.InstantiateWithParent(transform);
            mCenter = Tilemap; 
        }

        void UpDatePositions()
        {
            mUp.Position(new Vector3(AreaX * Tilemap.size.x, (AreaY + 1) * Tilemap.size.y)); // 设置Tilemap的位置
            mDown.Position(new Vector3(AreaX * Tilemap.size.x, (AreaY - 1) * Tilemap.size.y));

            mLeft.Position(new Vector3((AreaX - 1) * Tilemap.size.x, (AreaY + 0) * Tilemap.size.y));
            mRight.Position(new Vector3((AreaX + 1) * Tilemap.size.x, (AreaY + 0) * Tilemap.size.y));
            mUpLeft.Position(new Vector3((AreaX - 1) * Tilemap.size.x, (AreaY + 1) * Tilemap.size.y));
            mUpRight.Position(new Vector3((AreaX + 1) * Tilemap.size.x, (AreaY + 1) * Tilemap.size.y));
            mDownLeft.Position(new Vector3((AreaX - 1) * Tilemap.size.x, (AreaY - 1) * Tilemap.size.y));
            mDownRight.Position(new Vector3((AreaX + 1) * Tilemap.size.x, (AreaY - 1) * Tilemap.size.y));
            mCenter.Position(new Vector3((AreaX + 0) * Tilemap.size.x, (AreaY + 0) * Tilemap.size.y));
        }
	}
}
